package com.woniu.controller;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.AlipayConfig;
import com.alipay.api.domain.ExtUserInfo;
import com.alipay.api.response.AlipayTradeWapPayResponse;
import com.alipay.api.domain.AlipayTradeWapPayModel;
import com.alipay.api.domain.ExtendParams;
import com.alipay.api.domain.GoodsDetail;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import java.util.ArrayList;
import java.util.List;


public class testController {

    public static void main(String[] args) throws AlipayApiException {
        // 初始化SDK
        AlipayClient alipayClient = new DefaultAlipayClient(getAlipayConfig());

        // 构造请求参数以调用接口
        AlipayTradeWapPayRequest request = new AlipayTradeWapPayRequest();
        AlipayTradeWapPayModel model = new AlipayTradeWapPayModel();

        // 设置商户订单号
        model.setOutTradeNo("daniel82AAAA000032333361");

        // 设置订单总金额
        model.setTotalAmount("9.00");

        // 设置订单标题
        model.setSubject("大乐透");

        // 设置产品码
        model.setProductCode("QUICK_WAP_WAY");

        // 设置针对用户授权接口
        model.setAuthToken("appopenBb64d181d0146481ab6a762c00714cC27");

        // 设置用户付款中途退出返回商户网站的地址
        model.setQuitUrl("http://www.taobao.com/product/113714.html");

        // 设置订单包含的商品列表信息
        List<GoodsDetail> goodsDetail = new ArrayList<GoodsDetail>();
        GoodsDetail goodsDetail0 = new GoodsDetail();
        goodsDetail0.setGoodsName("ipad");
        goodsDetail0.setAlipayGoodsId("20010001");
        goodsDetail0.setQuantity(1L);
        goodsDetail0.setPrice("2000");
        goodsDetail0.setGoodsId("apple-01");
        goodsDetail0.setGoodsCategory("34543238");
        goodsDetail0.setCategoriesTree("124868003|126232002|126252004");
        goodsDetail0.setBody("特价手机");
        goodsDetail0.setShowUrl("http://www.alipay.com/xxx.jpg");
        goodsDetail.add(goodsDetail0);
        model.setGoodsDetail(goodsDetail);

        // 设置订单绝对超时时间
        model.setTimeExpire("2016-12-31 10:05:00");

        // 设置业务扩展参数
        ExtendParams extendParams = new ExtendParams();
        extendParams.setSysServiceProviderId("2088511833207846");
        extendParams.setHbFqSellerPercent("100");
        extendParams.setHbFqNum("3");
        extendParams.setIndustryRefluxInfo("{\"scene_code\":\"metro_tradeorder\",\"channel\":\"xxxx\",\"scene_data\":{\"asset_name\":\"ALIPAY\"}}");
        extendParams.setRoyaltyFreeze("true");
        extendParams.setCardType("S0JP0000");
        model.setExtendParams(extendParams);

        // 设置商户传入业务信息
        model.setBusinessParams("{\"mc_create_trade_ip\":\"127.0.0.1\"}");

        // 设置公用回传参数
        model.setPassbackParams("merchantBizType%3d3C%26merchantBizNo%3d2016010101111");

        // 设置商户的原始订单号
        model.setMerchantOrderNo("20161008001");

        // 设置外部指定买家
        ExtUserInfo extUserInfo = new ExtUserInfo();
        extUserInfo.setCertType("IDENTITY_CARD");
        extUserInfo.setCertNo("362334768769238881");
        extUserInfo.setName("李明");
        extUserInfo.setMobile("16587658765");
        extUserInfo.setFixBuyer("F");
        extUserInfo.setMinAge("18");
        extUserInfo.setNeedCheckInfo("F");
        extUserInfo.setIdentityHash("27bfcd1dee4f22c8fe8a2374af9b660419d1361b1c207e9b41a754a113f38fcc");
        model.setExtUserInfo(extUserInfo);

        request.setBizModel(model);
        // 第三方代调用模式下请设置app_auth_token
        // request.putOtherTextParam("app_auth_token", "<-- 请填写应用授权令牌 -->");

        AlipayTradeWapPayResponse response = alipayClient.pageExecute(request, "POST");
        // 如果需要返回GET请求，请使用
        // AlipayTradeWapPayResponse response = alipayClient.pageExecute(request, "GET");
        String pageRedirectionData = response.getBody();
        System.out.println(pageRedirectionData);

        if (response.isSuccess()) {
            System.out.println("调用成功");
        } else {
            System.out.println("调用失败");
            // sdk版本是"4.38.0.ALL"及以上,可以参考下面的示例获取诊断链接
            // String diagnosisUrl = DiagnosisUtils.getDiagnosisUrl(response);
            // System.out.println(diagnosisUrl);
        }
    }

    private static AlipayConfig getAlipayConfig() {
        // 应用私钥
        String privateKey  ="MIIEvgIBADANBgkqhkiG9w0BAQEFAASCB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j/k4TeLomg2xm++Izqk2vZ4Rr2ekyejj6NTDxQM+RdVzUznglJSwYh5RUFR3Qfy0HO2CcMI4q1LykpN9wf6GtG7X/GVGYzxL0MWjIDEQ1uVnNxOjrqPCD2TJGkwtd6Nqu6wIVPN1xoPE9xJ6EDILpA+YtHi7y1AoGBAI0cRfykTWN1Y4XpVX9atFqS2EvrnGLK0h9z3VwMNzRqVMMpSgA3Z+zOXJ8NP3xej4qtw+V1gFV17FZ3F+F7zreusv3QxYSttSE5B2Y3WLU5EHYZHOqpzxPw6jVuzqWMGxVrP87SlxEsufO3DektZ86O9OQMjyYpaaL7v0710rkjAoGBAJvb+YbXel0XzKLguF8AVRysh/M3MNznEnL8HYlP8Rie1kOyO/rMSxmwEef8aWyMQvsdmi7ePfBkmv7x/Q7ntG+TV/YFQOS/PRyrYc/5kZ/robVKa+EyBCTeZXV8xGtG2aXo5jxf/YiFeAMu2dSclBPYcXTJAMFVDgKSwkgAlgjY";
        String alipayPublicKey ="MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAp3NUKBdI9pZj0EzSfxmwg1uw/FLyIpMPOXdA42igoD0M1/OyO5tzQ9Zs9NA7vtsRKfh3TZmj5PygkeWaiEVMJDFIjpsmWWQPX481somUaf4flBKST4tRU5FpzG3MmucPY2YZt/2oamimRZsfIkIIo1+m77PJxXaCaL3BvvBRr5KVXyBSntPkk+8CkJd6ieg8Lx61gM4szmZJ8WB7ShBBifPhV1Vs69aHftuuSRP0epNuqkgRKCEx8qB3l4BL0jzexogxBhj2Z+o34lqXwEf4Kq8ybUtxAMcdba8hAjteee3FDNE1nRpuiOREOya1j6hNItqEE3iRRcRVAw9MTUS7DQIDAQAB";
        AlipayConfig alipayConfig = new AlipayConfig();
        alipayConfig.setServerUrl("https://openapi-sandbox.dl.alipaydev.com/gateway.do");
        alipayConfig.setAppId("9021000149652915"); //
        alipayConfig.setPrivateKey(privateKey);
        alipayConfig.setFormat("json");
        alipayConfig.setAlipayPublicKey(alipayPublicKey);
        alipayConfig.setCharset("UTF-8");
        alipayConfig.setSignType("RSA2");
        return alipayConfig;
    }
}